package com.lzq.springblog.controller;

import com.lzq.springblog.common.Result;
import com.lzq.springblog.common.ResultCode;
import com.lzq.springblog.model.BlogInfo;
import com.lzq.springblog.model.PageRequest;
import com.lzq.springblog.model.PageResult;
import com.lzq.springblog.service.BlogService;
import com.lzq.springblog.utils.JwtUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "博客接口")
@Slf4j
@RequestMapping("/blog")
@RestController
public class BlogController {

    @Autowired
    private BlogService blogService;
    @Autowired
    private BlogInfo blogInfo;

    @Operation(description = "获取博客列表")
    @GetMapping("/getListByPage")
    public Result getListByPage(PageRequest pageRequest) {
        log.info("执行getList方法");
        PageResult<BlogInfo> list = blogService.getBlogListByPage(pageRequest);
        log.info("获取到博客列表:{}",list);
        return Result.success(list);
    }

    @Operation(description = "获取博客详情")
    @GetMapping("/getBlogDetail")
    public Result getBlogDetail(Integer blogId,HttpServletRequest request) {
        log.info("执行getBlogDetail方法,接收参数id:{}",blogId);
        String jwt = request.getHeader("user_token");
        Integer userId = (Integer) JwtUtils.getUserIdFromToken(jwt).getData();
        if (userId==null || userId<1) {
            log.error("查询的用户信息不存在");
            return Result.failed(ResultCode.FAILED);
        }
        if (blogId==null) {
            return Result.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        Integer result = blogService.addViewCount(blogId);
        if (result<1) {
            log.error("获取博客阅读量失败");
        }
        BlogInfo blogInfo = blogService.getBlogDetail(blogId);
        log.info("获取博客详情:{}",blogInfo);
        if (blogInfo.getUserId()==userId) {
            blogInfo.setIsLoginUser(true);
        }
        return Result.success(blogInfo);
    }

    @Operation(description = "添加博客")
    @PostMapping("/add")
    public Result add(String title, String content, HttpServletRequest request) {
        String jwt = request.getHeader("user_token");
        log.info("开始执行add方法");
        Integer userId = (Integer) JwtUtils.getUserIdFromToken(jwt).getData();
        if (userId==null || userId<1) {
            log.error("查询的用户信息不存在");
            return Result.failed(ResultCode.FAILED);
        }
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setTitle(title);
        blogInfo.setContent(content);
        blogInfo.setUserId(userId);
        Integer result = blogService.insertBlog(blogInfo);
        if (result<1) {
            log.error("插入博客失败");
            return Result.failed(ResultCode.FAILED);
        }
        return Result.success(true);
    }

    @Operation(description = "更新博客")
    @PostMapping("/update")
    public Result update(@RequestBody BlogInfo blogInfo,HttpServletRequest request) {
        String jwt = request.getHeader("user_token");
        log.info("开始执行update方法");
        Integer userId = (Integer) JwtUtils.getUserIdFromToken(jwt).getData();
        if (userId==null || userId<1) {
            log.error("查询的用户信息不存在");
            return Result.failed(ResultCode.FAILED);
        }
        Integer result = blogService.updateBlog(blogInfo);
        if (result<1) {
            log.error("更新博客失败");
            return Result.failed(ResultCode.FAILED);
        }
        return Result.success(true);
    }

    @Operation(description = "删除博客")
    @PostMapping("/delete")
    public Result delete(Integer blogId) {
        log.info("开始执行delete方法");
        Integer result = blogService.deleteBlog(blogId);
        if (result<1) {
            log.error("删除博客失败");
            return Result.failed(ResultCode.FAILED);
        }
        return Result.success(true);
    }

    @Operation(description = "点赞博客")
    @PostMapping("/addLike")
    public Result addLike(Integer blogId,HttpServletRequest request) {
        log.info("开始执行addLike方法");
        String jwt = request.getHeader("user_token");
        Integer userId = (Integer) JwtUtils.getUserIdFromToken(jwt).getData();
        if (userId==null || userId<1) {
            log.error("查询的用户信息不存在");
            return Result.failed(ResultCode.FAILED);
        }
        Integer result = blogService.addBlogLike(userId,blogId);
        if (result<1) {
            log.error("增加点赞失败");
            return Result.failed(ResultCode.FAILED);
        }
        return Result.success(blogId);
    }

    @Operation(description = "取消点赞博客")
    @PostMapping("/cancelLike")
    public Result cancelLike(Integer blogId,HttpServletRequest request) {
        log.info("开始执行cancelLike方法");
        String jwt = request.getHeader("user_token");
        Integer userId = (Integer) JwtUtils.getUserIdFromToken(jwt).getData();
        if (userId==null || userId<1) {
            log.error("查询的用户信息不存在");
            return Result.failed(ResultCode.FAILED);
        }
        Integer result = blogService.cancelBlogLike(userId,blogId);
        if (result<1) {
            log.error("取消点赞失败");
            return Result.failed(ResultCode.FAILED);
        }
        return Result.success(blogId);
    }

    @Operation(description = "获取创作博客数")
    @GetMapping("/getUserArticleCounts")
    public Result getUserArticleCounts(Integer userId,HttpServletRequest request) {
        log.info("开始执行getUserArticleCounts方法");
        if (userId==null) {
            String jwt = request.getHeader("user_token");
            userId = (Integer) JwtUtils.getUserIdFromToken(jwt).getData();
        }
        if (userId<1) {
            log.error("查询的用户信息不存在");
            return Result.failed(ResultCode.FAILED);
        }
        Integer result = blogService.getUserArticleCounts(userId);
        if (result==null) {
            log.error("获取创作博客数失败");
            return Result.failed(ResultCode.FAILED);
        }
        return Result.success(result);
    }

    @Operation(description = "获取浏览博客数")
    @GetMapping("/getViewCounts")
    public Result getViewCounts(Integer blogId,HttpServletRequest request) {
        log.info("开始执行getViewCounts方法");
        Integer result = blogService.getViewCounts(blogId);
        if (result==null) {
            log.error("获取浏览博客数失败");
            return Result.failed(ResultCode.FAILED);
        }
        return Result.success(result);
    }

}
